package com.interview.core.attachment.repository;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.interview.core.attachment.entity.Attachment;
import com.interview.core.attachment.entity.Attachment_;
import com.interview.core.attachment.query.AttachmentConditionQuery;
import com.interview.employeeInfo.entity.EmployeeInfo;

@Repository
public class AttachmentRepository {
	@PersistenceContext
	private EntityManager entityManager;

	public Attachment findOne(String id) {
		Attachment attachment = entityManager.find(Attachment.class, id);
		return attachment;
	}

	public void save(Attachment attachment) {
		entityManager.persist(attachment);
	}

	public Attachment update(Attachment attachment) {
		return entityManager.merge(attachment);
	}

	public void delete(String id) {
		entityManager.remove(findOne(id));
	}

	public List<Attachment> findAll() {
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

		CriteriaQuery<Attachment> query = criteriaBuilder
				.createQuery(Attachment.class);

		Root<Attachment> root = query.from(Attachment.class);

		TypedQuery<Attachment> type = entityManager.createQuery(query);
		List<Attachment> attachments = type.getResultList();

		return attachments;
	}

	public List<Attachment> dynamicQuery(
			AttachmentConditionQuery attachmentConditionQuery) {

		String bizCode = attachmentConditionQuery.getBizCode();
		String contentType = attachmentConditionQuery.getContentType();
		String extension = attachmentConditionQuery.getExtension();
		String fileName = attachmentConditionQuery.getFileName();
		Long fileSize = attachmentConditionQuery.getFileSize();
		String moduleName = attachmentConditionQuery.getModuleName();
		String relativeFilePath = attachmentConditionQuery
				.getRelativeFilePath();
		String saveName = attachmentConditionQuery.getSaveName();

		
		List<Predicate> predicates = new ArrayList<Predicate>();

		
		
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

		CriteriaQuery<Attachment> query = criteriaBuilder
				.createQuery(Attachment.class);

		Root<Attachment> root = query.from(Attachment.class);

		if (bizCode != null) {
			Predicate predicateBizCode = criteriaBuilder.equal(
					root.get(Attachment_.bizCode), bizCode);

			predicates.add(predicateBizCode);
		}

		if (contentType != null) {
			Predicate predicateContentType = criteriaBuilder.equal(
					root.get(Attachment_.contentType), contentType);

			predicates.add(predicateContentType);

		}

		if (extension != null) {
			Predicate predicateExtension = criteriaBuilder.equal(
					root.get(Attachment_.extension), extension);
			predicates.add(predicateExtension);

		}

		if (fileName != null) {
			Predicate predicateFileName = criteriaBuilder.equal(
					root.get(Attachment_.fileName), fileName);
			predicates.add(predicateFileName);
		}
		if (fileSize != null) {
			Predicate predicateFileSize = criteriaBuilder.equal(
					root.get(Attachment_.fileSize), fileSize);
			predicates.add(predicateFileSize);
		}
		if (moduleName != null) {
			Predicate predicateModuleName = criteriaBuilder.equal(
					root.get(Attachment_.moduleName), moduleName);
			predicates.add(predicateModuleName);
		}
		if (relativeFilePath != null) {
			Predicate predicateRelativeFilePath = criteriaBuilder.equal(
					root.get(Attachment_.relativeFilePath), relativeFilePath);
			predicates.add(predicateRelativeFilePath);
		}
		if (saveName != null) {
			Predicate predicateSaveName = criteriaBuilder.equal(
					root.get(Attachment_.saveName), saveName);
			predicates.add(predicateSaveName);
		}

		query.where(criteriaBuilder.and(predicates
				.toArray(new Predicate[predicates.size()])));
		TypedQuery<Attachment> type = entityManager.createQuery(query);
		
		List<Attachment> attachments = type.getResultList();

		return attachments;
	}
}
